package com.maven.dao.impl;

import com.maven.dao.UserDao;
import com.maven.domain.User;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * maven使用 —— 查询所有用户
 *
 * @author innocent
 * @date 2019/1/14 16:35
 */
public class UserDaoImpl implements UserDao {



    public List<User> findAllUser() throws Exception{
        Connection conn = null;
        ResultSet resultSet = null;
        PreparedStatement prep = null;
        List<User> list = new ArrayList();

        try {
            //加载数据库驱动
            Class.forName("oracle.jdbc.driver.OracleDriver");
            //获取Connection
            conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.253:1521:ORCL","tt1109","123456");
            //SQL执行对象
            prep = conn.prepareStatement("select * from MYUSER");
            resultSet = prep.executeQuery();
            //将结果集封装为List
            while (resultSet.next()) {
                User user = new User();
                user.setUsername(resultSet.getString("username"));
                user.setAddress(resultSet.getString("address"));
                list.add(user);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            conn.close();
            prep.close();
            resultSet.close();
        }

        return list;
    }
}
